package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import database.Conn;

public class Service {
	private Connection connect =null;
	private PreparedStatement pstm=null;
	private String table="tblService";
	public boolean AddService(String name, int charge, String info){
		connect = Conn.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(name,charge,info) VALUES (?,?,?)");
			pstm.setString(1,name);
			pstm.setInt(2,charge);
			pstm.setString(3, info);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
		
	}
	public boolean DeleteService(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateService(int id,String name,int charge, String info){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET name=?,charge=?,info=?WHERE id=?");
			pstm.setString(1,name);
			pstm.setInt(2,charge);
			pstm.setString(3,info);
			pstm.setInt(5,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Service> GetAllService(){
		connect= Conn.getConnect();
		ArrayList<entity.Service> list = new ArrayList<entity.Service>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
				entity.Service service = new entity.Service();
				service.setId(rs.getInt("id"));
				service.setName(rs.getString("name"));
				service.setInfo(rs.getString("info"));
				service.setCharge(rs.getInt("charge"));
				list.add(service);
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public void GetServiceById(int id){
		connect= Conn.getConnect();
		ResultSet rs =null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs = pstm.executeQuery();
			// Lay thong tin gui den Entity
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

}
